package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.MyEvaluationEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/7.
 */

public class WePostEvaluationActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    BaseAdapter<MyEvaluationEntity.CommentsBean> baseAdapter;
    ArrayList<MyEvaluationEntity.CommentsBean> datta = new ArrayList<>();
    
    int page = 1;
    
    @Override
    public int getLayout() {
        return R.layout.activity_we_evaluation;
    }

    @Override
    public String initTitle() {
        return "我的评价";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),
                R.color.view_line,R.dimen.divider_space, LinearLayout.VERTICAL));
        
        baseAdapter = new BaseAdapter<MyEvaluationEntity.CommentsBean>(this,datta) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_we_evaluation;
            }

            @Override
            protected void getView(BaseViewHolder holder, MyEvaluationEntity.CommentsBean entity, int position) {
                SimpleDraweeView sdv_head = (SimpleDraweeView)holder.getView(R.id.sdv);
                sdv_head.setImageURI(UrlConst.baseUrl()+entity.getUser_img());
                holder.setText(R.id.tv_username,entity.getUsername());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_content,entity.getContent());
                
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv_shop);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOrder().getOrder_goods().getOriginal_img());
                holder.setText(R.id.tv_goodName,entity.getOrder().getOrder_goods().getGoods_name());
                holder.setText(R.id.tv_model,entity.getOrder().getOrder_goods().getSpec_key_name());
                holder.setText(R.id.tv_price,"￥"+entity.getOrder().getOrder_goods().getGoods_price());
                 
                String type = entity.getZan_num();
                if(type.equals("0")){
                    holder.setText(R.id.tv_evaluation,"已差评");
                }else if(type.equals("1")){
                    holder.setText(R.id.tv_evaluation,"已中评");
                }else if(type.equals("2")){
                    holder.setText(R.id.tv_evaluation,"已好评");
                }
                
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        
    }

    @Override
    public void initData() {
        getData(false);
    }
    
    public void getData(boolean isLoading){
        StoreRequest.evaluationList(this,page)
                .compose(DoTransform.<MyEvaluationEntity>applyScheduler(WePostEvaluationActivity.this,isLoading))
                .compose(this.<MyEvaluationEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<MyEvaluationEntity>(this) {
                    @Override
                    protected void onSuccess(MyEvaluationEntity entity) {
                        if(page==1){
                            datta.clear();
                        }
                        
                        datta.addAll(entity.getComments());
                        baseAdapter.setList(datta);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }
    
}
